package com.lqk.springbootminio.utils;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;

/**
 * @author liqiankun@nnuo.com
 * @date 2023/3/24 15:12
 * @description
 **/
public class MinioUtils {

    public static String spliceFilePath(String fileName) {
        String timeStr = DateTimeFormatter.ofPattern("yyyy-MM-dd").format(LocalDateTime.now());
        return timeStr + "/" + System.currentTimeMillis() + "."
                + StringUtils.substringAfterLast(fileName, ".");
    }

    /**
     * 获取bucketName
     *
     * @param contentType 文件类型
     * @param bucketNameMap 所有bucket映射
     * @return bucketName
     */
    public static String getBucketName(String contentType, Map<String, String> bucketNameMap) {
        String bucketName = bucketNameMap.get("file");
        if (contentType!=null){
            String type = contentType.split("/")[0];
            bucketName = bucketNameMap.get(type);
            if (StringUtils.isBlank(bucketName)) {
                // 默认为 file 存储bucketName
                bucketName = bucketNameMap.get("file");
            }
        }
        return bucketName;
    }
}
